<?php
/*
	用户
*/
namespace jy_kexie;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
defined('BY_JYA') or exit('error');
class project extends \plugin{
	function get_detail($id){
		$item=$this->get('project',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($id)));
		if(!$item){
			exi('指定项目不存在！','error');
		}
		$user=$this->pdo_get('project_cuser',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['user_id']));
		$item['user']=$user;
		return $item;
	}
	function export_detail($id,$to_file=false){
		$item=$this->get_detail($id);
		require ROOT_D . '/core/library/phpoffice/phpWord.php';
		$word = new PhpWord();
        $section = $word->addSection();
		$table = $section->addTable('');
        $section->addText('项目申报表', array('bold' => true,'name' => 'Tahoma', 'size' => 16));
        $section->addText("<w:br />");
		$table->addRow();
		$table->addCell(39*500)->addText("申报单位：".$item['user']['cname']);
        $section->addText("申报单位：".$item['user']['cname']);
        $section->addText("项目负责人：".$item['user']['username']);
		$section->addText("职务职称：".$item['user']['mobile']);
        $section->addText("联系电话：".$item['project_name']);
		$section->addText("一、立项依据和申报条件：", array('name' => 'Tahoma', 'size' => 15));
		$section->addText("二、项目主要内容：", array('name' => 'Tahoma', 'size' => 15));
		$section->addText("三、项目目标及预期成效：", array('name' => 'Tahoma', 'size' => 15));
		$section->addText("四、项目经费预算：", array('name' => 'Tahoma', 'size' => 15));
		$section->addText("五、项目实施步骤、进度计划：", array('name' => 'Tahoma', 'size' => 15));
		$section->addText("六、项目负责人及主要参加人员：", array('name' => 'Tahoma', 'size' => 15));
        //拼接成员信息
        foreach ($item['user_team'] as $value)
        {
            $tema_listStr.="序号：".$value['team_name']."<w:br />
            姓名：".$value['team_age']."<w:br />
            职务职称：".$value['team_sex']."<w:br />
            工作单位：".$value['team_jobtitle']."<w:br />
            项目中承担的主要工作：".$value['eduction_name']."<w:br />
            联系方式：".$value['team_school']."<w:br />";
        }
        $section->addText($tema_listStr);
        $section->addText("七、项目申报单位意见", array('name' => 'Tahoma', 'size' => 15));
		 $fileName = "项目-".$item['project_name'].date('Y-m-d H:i:s');
        $user_agent = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $user_agent) || preg_match("/rv:11.0/", $user_agent) || preg_match("/Edge/", $user_agent)) {
            $fileName = urlencode($fileName);
        } else {
            $fileName = iconv("utf-8", "gb2312", $fileName);
        }
		$write = IOFactory::createWriter($word, "Word2007");
		if($to_file){
			$write->save($to_file);
			return;
		}else{
			//exit('准备导出');
			ob_clean();
			header('Content-Type: application/vnd.ms-word');
			header("Content-Disposition: attachment;filename=".$fileName.".docx");
			header('Cache-Control: max-age=0');
			
			//
			$write->save('php://output'); // 文件通过浏览器下载
			exit;
		}
        
	}
}